/***************************************************************************
 *            udpclient.c
 *
 *  Thu May 31 09:08:42 2007
 *  Copyright  2007  kf701
 *  Email <kf701.ye AT gmail.com>
 ****************************************************************************/


#include "kf701.h"

#define SA struct sockaddr
	
int main()
{
	char *ip = "127.0.0.1";
	uint16_t port = 9877;
	
	debug_verbose = 4;
	
	int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if ( sock == -1 )
	{
		sys_err("socket error\n");
		return -1;
	}
	
	struct hostent *hent;
	hent = gethostbyname(ip);
	if (NULL == hent) 
	{
		sys_err("gethostbyname err\n");
		close(sock);
		return -1;
	}
	struct sockaddr_in servaddr;
	memset((char*) &(servaddr),0, sizeof((servaddr)));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(port);
	servaddr.sin_addr = *(struct in_addr *)hent->h_addr;
	socklen_t len = sizeof(servaddr);
		
	char buf[256];
	memset(buf, 0, sizeof(buf));
	int ret = sendto( sock, buf, sizeof(buf), 0, (SA*)&servaddr, len);
	if( ret != sizeof(buf) )
	{
		sys_err("sendto err\n");
		close( sock );
		return -1;
	}
	
	sys_log("send ok\n");
	close( sock );
	return 0;
}
